import { useEffect, useState } from "react";
import callFeedback from "../modalFeedback";
import { Grid, Button,Toast } from 'antd-mobile'
import {
  API,
  REDIRECT_COVER,
  REDIRECT_TITLE,
  REDIRECT_DESC,
  BASE_PATH,
} from "@/config";
import { PrizeComp, RuleComp, StuffComp } from "@/components/common";
import { appNavigate, appTypeCheck, checkENV, checkENV2, navigateTo } from "@/utils";
import Router from "next/router";
export const useToolbar = () => {
  const [env, setENV] = useState(undefined);
  let flag = 0;
      /**
   * 等待环境加载
   * @param ev
   */
  const waitForAuth = (ev) => {
    if (flag >= 30) {
      Toast.show({duration: 1000, content: `持续授权失败！请保证网络畅通、并在引力域的APP或小程序中打开`, position: `top`});
      return
    }
    setTimeout(() => {
      if (ev) {
        setENV(ev);
        return;
      }
      flag++;
      const resolve = checkENV();
      waitForAuth(resolve);
    }, 100);
  };
  useEffect(() => {
    waitForAuth()
  }, [])
  // const compHandler = 
  return {
    link: async (url) => {
      const env = await checkENV2();
      if (env === `mini`) {
        wx?.miniProgram.navigateTo({url: `/pages/subscribe/subscribe?id=17`})
      } else if (env === `app`) {
        const {type, value} = appTypeCheck(`https://head.uni.changan.com.cn/order/#/orderUNI/subscribe?id=19`) || {};
        appNavigate(type, value);
      }
    },
    navigate: url => navigateTo(url),
    // 打开规则
    rule: () => {
      console.log('huodong')
      const m = callFeedback({
        bodyClassName: `rule-modal close-at-center-bottom`,
        title: <img className="header-img" src={`${BASE_PATH}/img/game/rule-title.png`} alt="" />,
        content: <div className="inner"> <RuleComp /></div>,
        actions: <Button className="act-btn" onClick={()=>m.close()}>
        确 认
      </Button>,
        hideClose:true,
        closeOnAction: true,
        closeOnMaskClick: true,
      })
    },
    // 我的奖品
    prize: async () => {
      const m = callFeedback({
        bodyClassName: `act-feedback-modal close-at-center-bottom rule-modal`,
        title: <img className="header-img" width={200} src={`${BASE_PATH}/img/game/prize-modal-title.png`} />,
        content: <div className="inner"><PrizeComp /></div>,
        actions: <Grid columns={2} gap={20} >
        <Grid.Item style={{padding: '0 10px'}}>
        <Button shape={`rounded`}
                className="act-btn act-btn-bevel"
                onClick={e => {
                  m.close()
                  Router.replace('/landing')
                }}
        >
          返回首页
        </Button>
        </Grid.Item>
        <Grid.Item>
        <Button shape={`rounded`}
                className="act-btn act-btn-bevel"
                onClick={async e => {
                  m.close()
                  navigateTo(`/wheel`)
                }}
        >
          去抽奖
        </Button>
        </Grid.Item>
      </Grid>,
      hideClose:true,
        closeOnAction: true,
        closeOnMaskClick: true,
      })
    },
    // 点击分享
    pressShare: () => {
      // 记录分享
      if (env === `app`) {
        //
        const json = {
          shareTitle: REDIRECT_TITLE,
          // shareUrl: REDIRECT_URL,
          shareUrl: `https://head.uni.changan.com.cn/quanzi/#/zixunPreview?artId=1994`,
          shareImg: REDIRECT_COVER,
          shareDesc: REDIRECT_DESC,
          shareType: "3",
          isImg: "0",

          // isMiniProgram: "1",
          // userName: MINI_P_ID,
          // path: MINI_P_PATH,
          // miniprogramType: 0,
        }
        try {
          // 菜单分享
          window.UNIApp?.shareTo(JSON.stringify(json), 'shareCallBack')
        } catch (error) {
          Toast.show({content: `分享失败`})
        }

      }
      // 小程序分享
      else if (env === `mini`) {
        callFeedback({
          title: <div dangerouslySetInnerHTML={{__html: `请点击右上角「...」进行分享`}}>
          </div>
        })
      }
    },
    close: () => {
      setVisible(undefined)
    },
    stuff: () => {
      const m = callFeedback({
        bodyClassName: `act-feedback-modal close-at-center-bottom`,
        content: <div className="inner"><StuffComp /></div>,
        actions: <></>,
        closeOnAction: true,
        closeOnMaskClick: true,
      })
    }
  }
}